package com.yang.leather.common.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.LazyDynaBean;

public class RequestUtil {


	public static void responseOut(String encoding, String data, HttpServletResponse response) {
		response.setContentType("text/xml; charset=" + encoding);
		try {
			PrintWriter pw = response.getWriter();
			pw.print(data);
	        pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Map<String, String> getMapByRequest(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration enu = request.getParameterNames();
		while(enu.hasMoreElements()) {
            String paraName = (String)enu.nextElement();
            String paraValue = request.getParameter(paraName);
            if(paraValue!=null && !"".equals(paraValue)){
            	map.put(paraName, paraValue);
            }
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public static DynaBean getDynaBeanByRequest(HttpServletRequest request) {
		DynaBean bean = new LazyDynaBean();
		Enumeration enu = request.getParameterNames();
		while(enu.hasMoreElements()) {
            String paraName = (String)enu.nextElement();
            String paraValue = request.getParameter(paraName);
            bean.set(paraName, paraValue);
		}
		return bean;
	}

}
